package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.TemplateFolderBO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.TemplateFolderDao;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class TemplateFolderService {
    @Resource
    private TemplateFolderDao templateFolderDao;
    
    public TemplateFolderBO queryBy(String id) {
        return templateFolderDao.queryBy(id);
    }
    
    public List<TemplateFolderBO> queryList(TemplateFolderBO bo) {
        return templateFolderDao.queryList(bo);
    }
    
    public PageInfo<TemplateFolderBO> queryPage(TemplateFolderBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(templateFolderDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(TemplateFolderBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return templateFolderDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return templateFolderDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(TemplateFolderBO bo) {
        return templateFolderDao.delete(bo);
    }

    @WriteTransactional
    public void saveList(List<TemplateFolderBO> folderList) {
        if (folderList == null) return;

        folderList.forEach(folder -> {
            if (templateFolderDao.queryBy(folder.getId()) != null) {
                folder.setUpdateTime(System.currentTimeMillis());
                templateFolderDao.update(folder);
            } else {
                folder.setUpdateTime(System.currentTimeMillis());
                folder.setCreateTime(System.currentTimeMillis());
                templateFolderDao.insert(folder);
            }
        });
    }

    public long queryTotal(TemplateFolderBO bo) {
        PageHelper.startPage(0, 0);
        return new PageInfo<>(templateFolderDao.queryList(bo)).getTotal();
    }

    @WriteTransactional
    public int updateParentId(TemplateFolderBO bo) {
        return templateFolderDao.updateParentId(bo);
    }

    public boolean isParent(String id, String parentId) {
        if (Tool.isBlank(parentId) || Constants.ROOT_ID.equals(parentId)) {
            return false;
        }

        if (Tool.isBlank(id) || Constants.ROOT_ID.equals(id)) {
            return false;
        }

        TemplateFolderBO bo = templateFolderDao.queryBy(id);
        if (bo == null) {
            return false;
        } else if (bo.getParentId().equals(parentId)) {
            return true;
        }

        return isParent(bo.getParentId(), parentId);
    }
}
